package com.it.admin.impl;

import com.it.admin.AdminOrderService;
import com.it.bean.R;
import com.it.bean.RestStatusEnum;
import com.it.dao.OrderMapper;
import com.it.pojo.Order;
import com.it.pojo.params.order.AdminOrderQuery;
import com.it.pojo.vo.order.AdminOrderVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class AdminOrderServiceImpl implements AdminOrderService {

    @Resource
    private OrderMapper orderMapper;

    public static void main(String[] args) {
        System.out.println("UUID.randomUUID().toString() = " + UUID.randomUUID().toString());
        long leastSignificantBits = cn.hutool.core.lang.UUID.randomUUID().getMostSignificantBits();
        long leastSignificantBits1 = leastSignificantBits * -1;
        System.out.println("leastSignificantBits1 = " + leastSignificantBits1);

    }

    @Override
    public Map<String, Object> listOrders(AdminOrderQuery adminOrderQuery) {
        System.out.println("adminOrderQuery = " + adminOrderQuery);
        Integer orderId = adminOrderQuery.getOrderId();
        Integer sendStatus = adminOrderQuery.getSendStatus();
        Integer status = adminOrderQuery.getStatus();
        Integer limit = adminOrderQuery.getLimit();
        Integer start = (adminOrderQuery.getPageNum() - 1) * limit;
        System.out.println("start = " + start);
        System.out.println("limit = " + limit);
        Map<String, Object> map = new HashMap<>();
        int total = orderMapper.listOrdersTotal(orderId, sendStatus, status);
        map.put("total", total);
        List<AdminOrderVO> list = orderMapper.listOrders(orderId, sendStatus, status, start, limit);
        map.put("orderList", list);

        return map;

    }

    @Override
    public R<?> updateOrderStatus(Long orderId) {
        Order order = orderMapper.selectById(orderId);
        System.out.println("order = " + order);
        if (order != null) {
            if (order.getStatus() == 0) {
                return R.error(RestStatusEnum.ORDER_SAATUS);
            }
            if (order.getSendStatus() == 1) {
                return R.error(RestStatusEnum.ERROR_SEND_STATUS);
            }
            Date date = new Date();
            order.setGmtModified(date);
            order.setSendStatus(1);
            System.out.println("order = " + order);
            if (orderMapper.updateById(order) > 0) {
                return R.success();
            }
        }
        return R.error();
    }
}
